package com.augus.admin.config;

import com.augus.admin.interceptor.LoginInterceptor;
import com.augus.admin.interceptor.RedisUrlCountInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 重写拦截器 - 自定义拦截器放行
 * <p>
 * 1、编写一个拦截器实现HandlerInterceptor接口
 * 2、拦截器注册到容器种(实现WebMvcConfigurer的addInterceptors)
 * 3、指定路径规则(如果是拦截所有,静态资源也会被拦截) --- (还可以精确拦截)
 * <p>
 * EnableWebMvc 全面接管
 * 1.静态资源 视频解析器 欢迎页面,全部失效
 */
// @EnableWebMvc
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

  /**
   * Filter、Interceptor 几乎拥有相同的功能？
   * 1、Filter是Servlet定义的原生组件。好处，脱离Spring应用也能使用
   * 2、Interceptor是Spring定义的接口。可以使用Spring的自动装配等功能
   */
//  @Autowired
  RedisUrlCountInterceptor redisUrlCountInterceptor;

  /**
   * 定义静态资源行为
   * @param registry
   */
  /*@Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    */

  /**
   * 访问 /aa/** 所有请求都去 classpath://static/ 下面进行匹配
   *//*
    registry.addResourceHandler("/aa/**")
        .addResourceLocations("classpath:/static/");

  }*/
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LoginInterceptor())
        .addPathPatterns("/**") // 所有请求都被拦截包括静态资源
        .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**", "/aa/**"); // 放行请求

   /* registry.addInterceptor(redisUrlCountInterceptor)
        .addPathPatterns("/**")
        .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**","/aa/**");*/
  }

    /*@Bean
    public WebMvcRegistrations webMvcRegistrations(){
        return new WebMvcRegistrations(){
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return null;
            }
        };
    }*/
}
